﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "Actor/CreateBossTrigger.h"

#include "Character/PlayerCharacter.h"
#include "GameMode/CombatGameMode.h"
#include "Helper/DebugHelper.h"
#include "Kismet/GameplayStatics.h"


// Sets default values
ACreateBossTrigger::ACreateBossTrigger()
{
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;

	BoxComponent = CreateDefaultSubobject<UBoxComponent>("BoxComponent");
	BoxComponent->SetupAttachment(RootComponent);
}

// Called when the game starts or when spawned
void ACreateBossTrigger::BeginPlay()
{
	Super::BeginPlay();
	BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &ACreateBossTrigger::OnComponentBeginOverlap);
}

// Called every frame
void ACreateBossTrigger::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

void ACreateBossTrigger::OnComponentBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
                                                 UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
                                                 const FHitResult& SweepResult)
{
	APlayerCharacter* Player = Cast<APlayerCharacter>(OtherActor);
	if (Player && !HaveCreateBoss)
	{
		const ACombatGameMode* GameMode = Cast<ACombatGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
		if (GameMode && GameMode->CanCreateBoss)
		{
			if (GameMode->CreateBossPoint)
			{
				GameMode->CreateBossPoint->CreateBoss();
				HaveCreateBoss = true;
			}
		}
	}
}
